package com.open.party.cloud.view.home.eJia;

import android.os.Bundle
import android.view.View
import cn.android.x.model.SuperItemBean
import com.sinothk.android.utils.inters.OnSuperListener
import cn.sinothk.hussars.views.scrollViewGridView.IconTextBean
import cn.sinothk.hussars.views.scrollViewGridView.ScrollViewGridView
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.PageReq
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusRecycleViewTitleBaseActivity
import cn.android.x.plugins.webView.CommWebDetailsActivity
import com.open.party.cloud.view.comm.view.scrollViewGridView.FunItemAdapter
import com.open.party.cloud.R
import com.open.party.cloud.model.*
import com.open.party.cloud.model.data.LocalCache
import com.open.party.cloud.view.freePhoto.FreePhotosAddActivity
import com.open.party.cloud.view.freePhoto.FreePhotosMainActivity
import com.open.party.cloud.view.home.article.ArticleDetailsActivity
import com.open.party.cloud.view.home.article.ArticleMainActivity
import com.open.party.cloud.view.home.article.adapter.ArticleListAdapter
import com.open.party.cloud.view.home.dangWuMiShu.DwmsDiaoChaWenJuanListActivity
import com.open.party.cloud.view.home.dangWuMiShu.DwmsTouPiaoGuanLiListActivity
import com.open.party.cloud.view.home.jiCengDangJian.partyService.ZyzServiceMainActivity
import com.open.party.cloud.view.login.LoginActivity
import com.open.party.cloud.view.msg.MsgDetailActivity
import com.open.party.cloud.view.msg.adapter.MsgMainListAdapter
import com.open.party.cloud.view.task.TaskListForAllActivity
import com.open.party.cloud.viewModel.ArticleViewModel
import com.open.party.cloud.viewModel.MsgViewModel
import com.sinothk.android.utils.XUtils
import com.sinothk.rxretrofit.bean.PageVo
import com.sinothk.widget.loadingRecyclerView.extend.LoadingRecycleViewHeader
import kotlinx.android.synthetic.main.comm_activity_load_recycle_view_load_tip_shadow.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

open class EJiaMainActivity : AppEventBusRecycleViewTitleBaseActivity<MsgVo>(), OnSuperListener<MsgEntity> {
    private var viewModel: MsgViewModel? = null

    private var adapter: MsgMainListAdapter? = null

    override fun getLayoutResId(): Int = R.layout.comm_activity_load_recycle_view_load_tip_shadow

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("党建E家")
        viewModel = MsgViewModel()

        initView()
    }

    private fun initView() {
        initRecycleLinearView(loadingRecyclerView)
        loadingRecyclerView.addHeaderView(createHeaderView())

        adapter = MsgMainListAdapter(this)
        loadingRecyclerView.adapter = adapter
        adapter?.setOnSuperListener(this)
        loadingRecyclerView.setLoadingMoreEnabled(false)

        loadingTipView.setRetryListener {
            refresh()
        }
        refresh()
        loadingTipView.showContent()
    }

    private fun refresh() {
//        loadingTipView.showLoading("努力加载中")
        refreshData()
    }

    override fun loadData(pageVo: PageVo<MsgVo>) {
//        val pageReq = PageReq<MsgVo>()
//        pageReq.pageNo = pageVo.pageNum
//        pageReq.pageSize = pageVo.pageSize
//        val vo = MsgVo()
////        vo.childrenType = DictionaryBean("JCDJ_DJYJ", "")
//        pageReq.data = vo
//
//        viewModel!!.getPartyLifeNoticeList(pageReq)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<AppPageData<MsgEntity>>) {
        if (result.eventType != "getPartyLifeNoticeList") {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {
                    setPageData(result.data)
                } else {
                    loadingTipView.showError("数据异常")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                loadingTipView.showError("Token过期，请重新登录")
            }
            else -> {
                loadingTipView.showError(result.msg)
            }
        }

        stopLoading(loadingRecyclerView, loadType)
    }

    private fun setPageData(appPageData: AppPageData<MsgEntity>) {
        if (loadType == LoadType.REFRESH) {
            if (appPageData.records.size == 0) {
//                loadingTipView.showEmpty("暂无数据", R.drawable.no_data_01)
                loadingRecyclerView.setLoadingMoreEnabled(false)
                adapter!!.setData(ArrayList())
                loadingTipView.showContent()
            } else {
                adapter!!.setData(appPageData.records!!)
                loadingTipView.showContent()
            }
        } else {
            if (appPageData.records.size == 0) {
                tip("没有更多数据了")
            } else {
                adapter!!.updateData(appPageData.records!!)
                loadingTipView.showContent()
            }
        }

        // 设置：通用
        if (appPageData.current < appPageData.pages) {
            loadingRecyclerView.setLoadingMoreEnabled(true)
        } else {
            loadingRecyclerView.setLoadingMoreEnabled(false)
            loadingRecyclerView.setNoMore(true)
        }

    }

    override fun onClick(position: Int, itemData: MsgEntity, flag: String?) {
//        XUtils.intent().openActivity(this, ArticleDetailsActivity::class.java)
//            .putStringExtra("id", itemData.id)
//            .start()
        XUtils.intent().openActivity(this, MsgDetailActivity::class.java)
            .putSerializableExtra("notice", itemData)
            .start()
    }

    /**
     * 头部
     */
    private fun createHeaderView(): View {
        val headerView: View = LoadingRecycleViewHeader.getViewByLayoutId(this, R.layout.e_jia_main_list_header)
        val adapter = FunItemAdapter(this, getIconTxt())

        val gridView: ScrollViewGridView = headerView.findViewById(R.id.gridView)
        gridView.adapter = adapter

        adapter.setOnSuperListener { _, itemData, _ ->


            when (itemData.text) {
                "调查问卷" -> {
                    XUtils.intent().openActivity(this, DwmsDiaoChaWenJuanListActivity::class.java).start()
                }
            }

            // 需要登录项
            if (!LocalCache.getAutoLogin()) {
                // 登录检测
                XUtils.intent().openActivity(this, LoginActivity::class.java)
                    .start()
                return@setOnSuperListener
            }

            when (itemData.text) {
                "组织生活" -> {
                    // 需要党组织管理员
                    if ("PARTY_ADMIN" != LocalCache.getLoginRole()) {
                        XUtils.toast().show("党组织管理员专用")
                        return@setOnSuperListener
                    }
                    XUtils.intent().openActivity(this, PartyLifeMainActivity::class.java).start()
                }
//                "民生通道" -> {
//                    CommWebDetailsActivity.start(this, "民生通道", "https://zwfw.guizhou.gov.cn/?areacode=520000")
//                }
                "网上工作调度" -> {
                    // 需要登录项
                    if (!LocalCache.getAutoLogin()) {
                        // 登录检测
                        XUtils.intent().openActivity(this, LoginActivity::class.java)
                            .start()
                        return@setOnSuperListener
                    }

                    // 游客不能用
                    if ("TOURIST" == LocalCache.getLoginRole()) {
                        XUtils.toast().show("你没有权限")
                        return@setOnSuperListener
                    }

                    XUtils.intent().openActivity(this, TaskListForAllActivity::class.java).start()
                }
                "党员随手拍" -> {
                    // 需要登录项
                    if (!LocalCache.getAutoLogin()) {
                        // 登录检测
                        XUtils.intent().openActivity(this, LoginActivity::class.java)
                            .start()
                        return@setOnSuperListener
                    }

//                    // 游客不能用
//                    if ("TOURIST" == LocalCache.getLoginRole()) {
//                        XUtils.toast().show("你没有权限")
//                        return@setOnSuperListener
//                    }

                    XUtils.intent().openActivity(this, FreePhotosMainActivity::class.java).start()
                }

                "党员服务" -> {
                    XUtils.intent().openActivity(this, ZyzServiceMainActivity::class.java).start()
                }

                "投票管理"->{
                    XUtils.intent().openActivity(this, DwmsTouPiaoGuanLiListActivity::class.java).start()
                }
            }
        }
        return headerView
    }

    private fun getIconTxt(): ArrayList<IconTextBean> {
        val listData = ArrayList<IconTextBean>()
        listData.add(IconTextBean(R.drawable.jcdj_dyfw, "党员服务"))
//        listData.add(IconTextBean(R.drawable.zgdt_szyw, "组织生活"))
        listData.add(IconTextBean(R.drawable.zgdt_zgdt, "网上工作调度"))
        listData.add(IconTextBean(R.drawable.zgdt_tszs, "党员随手拍"))
//        listData.add(IconTextBean(R.drawable.zgdt_xjdx, "视频会议"))
//        listData.add(IconTextBean(R.drawable.zgdt_tzgg, "党建活动"))
//        listData.add(IconTextBean(R.drawable.zgdt_tzgg, "民生通道"))

        listData.add(IconTextBean(R.drawable.dwms_tpgl, "投票管理"))
        listData.add(IconTextBean(R.drawable.dwms_dcwj, "调查问卷"))

        return listData
    }


}

